package com.book.service.impl;


import com.book.dao.BookClassDao;
import com.book.dao.BookClassesDao;
import com.book.dao.BookDao;
import com.book.dao.BookImageDao;
import com.book.dto.BookDto;
import com.book.dto.BookPage;
import com.book.entity.Book;
import com.book.service.BookService;
import com.book.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
//这个类错误码统一4001开始
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;
    @Autowired
    private BookClassDao bookClassDao;
    @Autowired
    private BookClassesDao classesDao;//具体的哪本书什么分类表
    @Autowired
    private BookImageDao bookImageDao;//书籍图片表

    @Override
    public ResponseResult getList(BookPage bookPage){
        try {
            PageHelper.startPage(bookPage.getCurrentPage(), bookPage.getPageSize());
            List<BookDto> list = bookDao.selectBooksWithDetailsAndPagination(
                    bookPage.getBookName(),bookPage.getAuthor(),bookPage.getStartPrice(),bookPage.getEndPrice(),bookPage.getPriceSort(),
                    bookPage.getClassId()
            );
            PageInfo<BookDto> bookDtoPage = new PageInfo<>(list);

            if(bookPage.getCurrentPage()>bookDtoPage.getPages()){
                PageHelper.startPage(bookDtoPage.getPages(), bookPage.getPageSize());
                list = bookDao.selectBooksWithDetailsAndPagination(
                        bookPage.getBookName(),bookPage.getAuthor(),bookPage.getStartPrice(),bookPage.getEndPrice(),bookPage.getPriceSort(),
                        bookPage.getClassId()
                );
                bookDtoPage = new PageInfo<>(list);
            }
//            System.out.println(bookDtoPage);
            return new ResponseResult(200,"查询成功",bookDtoPage);//
        }catch (Exception e){
            return new ResponseResult(4001,"查询失败："+e.getMessage());
        }

    }

    public ResponseResult getClassList() {
//        QueryWrapper<BookClass> qw = new QueryWrapper<>();
//        System.out.println(bookClassDao.selectList(qw));
        try {
            return new ResponseResult(200,"获取书籍类名信息成功",bookClassDao.selectList(null));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(4002,"获取分类列表失败");
        }

    }


    /**
     * 查询某本书的具体的信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult getBookById(int id) {//这是书的id,查询某本书的id
        try {
            Book book = bookDao.selectById(id);//根据id查到这本书
            BookDto bookDto = new BookDto();
            BeanUtils.copyProperties(book, bookDto);//把查出来的book放到这里bookdto里面
            //然后查出这本书的所有的类名
            //todo 这里可能有bug，不小心删除了一个什么东西，记得到时候验证一下前台
            bookDto.setClassNames(classesDao.getClasses(id));
            //查出所有的图片
            bookDto.setImgUrls(bookImageDao.getAllImageById(id));

            return new ResponseResult(200,"成功",bookDto);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(4003,"获取书籍详情失败");
        }

    }

    /**
     * 管理员单独查看所有书籍
     * @param bookPage
     * @return
     */
    @Override
    public ResponseResult getBooks(BookPage bookPage) {
        try {
            PageHelper.startPage(bookPage.getCurrentPage(), bookPage.getPageSize());
            List<BookDto> list = bookDao.getBooks(bookPage.getBookName());
            PageInfo<BookDto> bookDtoPage = new PageInfo<>(list);

            if(bookPage.getCurrentPage()>bookDtoPage.getPages()){
                PageHelper.startPage(bookDtoPage.getPages(), bookPage.getPageSize());
                list = bookDao.getBooks(bookPage.getBookName());
                bookDtoPage = new PageInfo<>(list);
            }
//            System.out.println(bookDtoPage);
            return new ResponseResult(200,"查询成功",bookDtoPage);//
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(4004,"获取书籍列表失败");
        }
    }

    /**
     * 管理员删除书籍
     */
    @Override
    public ResponseResult deleteBook(int id) {
        try {
            bookDao.deleteById(id);
            return new ResponseResult(200,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(4005,"删除书籍失败");
        }
    }

    /**
     * 管理员新增书籍
     */
    @Override
    public ResponseResult addBook(Book book) {
//        System.out.println(book);
        try {
            if(book.getId()==0){
                bookDao.insert(book);
            }else {
                bookDao.updateById(book);
            }
            return new ResponseResult(200,"保存书籍信息成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(4006,"新增书籍失败");
        }
    }


    /**
     * 管理员根据id获取具体书籍信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult getOneBook(int id) {
        try {
            return new ResponseResult(200,"保存书籍信息成功",bookDao.selectById(id));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(4007,"新增书籍失败");
        }
    }

    /**
     * 管理员根据id获取某本书的具体书籍分类信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult getBookClassesById(int id) {
        try {
            return new ResponseResult(200,"获取书籍已有分类信息成功",classesDao.getBookClassesById(id));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(4008,"获取书籍已有分类信息成功");
        }
    }
}
